﻿using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;

namespace Yoozoo.Gameplay.Liberty.VehicleController
{
    public class VehicleInteriorComponent : MonoBehaviour
    {

        [Serializable]
        public class Interior
        {
            public MeshRenderer renderer;
            public List<Material> materials;

            public void HideInterior()
            {
                if (materials.Count > 1)
                {
                    Material[] mats = new Material[materials.Count - 1];
                    for (int i = 0; i < materials.Count - 1; i++)
                    {
                        mats[i] = materials[i];
                    }
                    renderer.sharedMaterials = mats;
                }
            }

            public void ShowInterior()
            {
                Material[] mats = materials.ToArray();
                renderer.sharedMaterials = mats;
            }
        }
        
        public string interiorMatName = "mat_RPG_CAR_inner_01";
        
        public List<Interior> interiors;


        public void HideInteriors()
        {
            if(interiors == null) return;
            foreach (var interior in interiors)
            {
                interior.HideInterior();
            }
        }

        private void OnEnable()
        {
            
        }

        public void ShowInteriors()
        {
            if(interiors == null || this.enabled==false) return;
            foreach (var interior in interiors)
            {
                interior.ShowInterior();
            }
        }

        public void FindInteriors(bool autoSave = true)
        {
#if UNITY_EDITOR
            interiors = new List<Interior>();
            var meshRenderers = GetComponentsInChildren<MeshRenderer>(true);
            for (int i = 0; i < meshRenderers.Length; i++)
            {
                var ms = meshRenderers[i];

                if (ms.sharedMaterials != null && ms.sharedMaterials.Length > 0)
                {
                    try
                    {
                        if (ms.sharedMaterials[ms.sharedMaterials.Length - 1] != null && ms.sharedMaterials[ms.sharedMaterials.Length - 1].name.Contains("mat_RPG_CAR_inner_01"))
                        {
                            interiors.Add(new Interior()
                            {
                                renderer = ms,
                                materials = ms.sharedMaterials.ToList()
                            });
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.LogError("error: " + e.Message + " " + e.StackTrace);
                    }
                  
                }
            }

            if (autoSave)
            {
                var stage = UnityEditor.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage();
                var prefab = PrefabUtility.GetNearestPrefabInstanceRoot(this.gameObject);
                if (prefab != null)
                {
                    if (stage == null)
                    {
                        PrefabUtility.ApplyPrefabInstance(prefab, InteractionMode.AutomatedAction);
                        Selection.activeGameObject = gameObject;
                        //EditorSceneManager.SaveScene(SceneManager.GetActiveScene());
                    }
                    else
                    {
                        EditorUtility.SetDirty(prefab);
                    }

                }
                else
                {
                    if (stage != null)
                        EditorUtility.SetDirty(gameObject);
                    else
                        PrefabUtility.SavePrefabAsset(gameObject);
                }
            }
            
#endif
        }
        
    }
}